const webpack = require('webpack');
const path = require('path');
const htmlPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const PostCssNext = require('postcss-cssnext');
const PostCssImport = require('postcss-import');
const precss = require('precss');

const config = {
  entry: {
    index: './src/home/index.js',
  },
  output: {
    path: path.join(__dirname, 'dist'),
    filename: '[name]-[hash].js',
    publicPath: '',
    crossOriginLoading: "anonymous",
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        loader: 'babel-loader',
        options: {
          presets: ["env"],
        },
        exclude: /node_modules/,
      },
      {
        test: /\.[p]?css$/,
        use: ExtractTextPlugin.extract({
          fallback: 'style-loader',
          use: [
            {
              loader: 'css-loader',
              options: {
                minimize: process.env.NODE_ENV === 'production',
              },
            },
            {
              loader: 'postcss-loader',
              options: {
                plugins: [PostCssImport, PostCssNext({
                  browsers: [
                    ">1%",
                    "last 4 versions",
                    "not ie < 8"
                  ],
                }), precss],

              },
            }
          ],
          allChunks: true,
        }),
      },
      {
        test: /\.(png|jpg|gif)$/,
        loader: 'file-loader?name=image/[name]-[hash].[ext]'
      },
      {
        test: /\.(woff|woff2|ttf|eot|svg)$/,
        loader: 'url-loader?limit=10000'
      },
    ],
  },
  target: 'web',
  stats: 'errors-only',
  devServer: {
    contentBase: path.join(__dirname, 'dist'),
    compress: true,
    historyApiFallback: true,
    host: "0.0.0.0",
    hot: false,
    disableHostCheck: true,
    noInfo: false,
    open: true,
    openPage: 'index.html',
  },
  cache: false,
  plugins: [
    // new webpack.HotModuleReplacementPlugin(),
    new ExtractTextPlugin('[name].[hash].css'),
    new htmlPlugin({
      template: './src/home/index.html',
      filename: `index.html`,
      hash: true,
      cache: true,
      inject: 'body',
      chunks: ['index'],
    }),
  ]
};

'advertiser|developer|about|case|join'.split('|').forEach(name => {
  config.entry[name] = `./src/${name}/index.js`;
  config.plugins.push(
    new htmlPlugin({
      template: `./src/${name}/index.html`,
      filename: `${name}.html`,
      hash: false,
      inject: 'body',
      chunks: [name],
    })
  );
});

if (process.env.NODE_ENV === 'production') {
  config.module.rules.push({
    test: /\.js$/,
    enforce: 'post',
    loader: 'es3ify-loader',
  });
  config.plugins.push(
    new webpack.optimize.UglifyJsPlugin({
      compress: {
        properties: false,
        warnings: false,
        drop_console: false,
      },
      output: {
        beautify: true,
        quote_keys: true,
      },
      sourceMap: false,
      mangle: {
        screw_ie8: false,
        except: ['e']
      }
    })
  );
}

module.exports = config;
